// Copyright 2004 The Apache Software Foundation
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain token copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace AspectSharp.Lang.AST
{
	using System;
    using System.IO;

	[Flags]
	public enum PointCutFlags
	{
		Unspecified,
		Method = 1,
		Property = 2,
		PropertyRead = 4,
		PropertyWrite = 8,
	}

	/// <summary>
	/// Summary description for PointCutDefinition.
	/// </summary>
	[Serializable]
	public class PointCutDefinition : NodeBase
	{
        public MethodSignature Method { get; set; }
        public PointCutFlags Flags { get; private set; }
        public InterceptorDefinitionCollection Advices { get; private set; }

        public PointCutDefinition(PointCutFlags flags, string signature):base(LexicalInfo.Empty)
        {
            Flags = flags;

            ParseSignature(signature);
        }

        private void ParseSignature(string signature)
        {
            StringReader reader = new StringReader(signature);
            AspectLanguageLexer lexer = new AspectLanguageLexer(reader);
            AspectParser parser = new AspectParser(lexer);
            parser.ParsePointcutSignature(this);
        }

		public PointCutDefinition(LexicalInfo info, PointCutFlags flags) : base(info)
		{
            Method = AllMethodSignature.Instance;
            Flags = flags;
            Advices = new InterceptorDefinitionCollection();
		}

       
		public override bool Equals(object other)
		{
            return Equals(other as PointCutDefinition);
		}

        public bool Equals(PointCutDefinition otherCut)
        {
            if (otherCut == null)
                return false;

            if (otherCut.Flags.Equals(Flags) &&
                otherCut.Method.Equals(Method))
                return true;

            return false;
        }
	
		public override int GetHashCode()
		{
			/// Doh!!
			return ((int) Flags) ^ Method.GetHashCode();
		}

		public override void Accept(IVisitor visitor)
		{
			visitor.OnPointCutDefinition(this);
		}
	}
}
